﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kaiser.Extensions;
using Kaiser.Infrastructure.User;
using Kaiser.Models.ViewModels;

namespace Kaiser.ActionFilters.Action
{
    public class UserActionFilter : IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var model = filterContext.Controller.ViewData.Model as ViewModel;

            if (model != null)
            {
                IUser user = null;

                if (filterContext.HttpContext.Items.Contains(typeof(IUser).FullName))
                {
                    user = filterContext.HttpContext.Items[typeof(IUser).FullName] as IUser;
                }

                if (user != null && user.IsAuthenticated)
                {
                    model.User = new UserViewModel(user);
                }
                else
                {
                   // model.User = new UserViewModel(filterContext.HttpContext.Request.Cookies.GetAnonymousUser());
                }
            }
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
        }
    }
}
